/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package au.edu.swin.swinvehicle.vehiclebooking;

import au.edu.swin.swinvehicle.bookingtype.BookingTypeEntity;
import au.edu.swin.swinvehicle.corporateclient.CorporateClientEntity;
import au.edu.swin.swinvehicle.driver.DriverEntity;
import au.edu.swin.swinvehicle.eventtype.EventTypeEntity;
import au.edu.swin.swinvehicle.vehicle.VehicleEntity;
import au.edu.swin.swinvehicle.vehicletype.VehicleTypeEntity;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author Administrator
 */
@Stateless
public class VehicleBookingManagerBean implements VehicleBookingManagerRemote {

    @PersistenceContext
    private EntityManager em;

    public void addVehicleBooking(VehicleBookingDetailedTO to) throws VehicleBookingManagerException {
        try {
            VehicleBookingEntity vb = new VehicleBookingEntity();
            vb.setData(to);
            vb.setBookingType(this.em.find(BookingTypeEntity.class, to.getBookingType().getId()));
            vb.setEventType(this.em.find(EventTypeEntity.class, to.getEventType().getId()));
            vb.setVehicleType(this.em.find(VehicleTypeEntity.class, to.getVehicleType().getId()));
            if (to.getCorporateClient() != null && to.getCorporateClient().getId() != null)
            {
                vb.setCorporateClient(this.em.find(CorporateClientEntity.class, to.getCorporateClient().getId()));
            }
            vb.setCompleted(Boolean.FALSE);
            vb.setDeleted(Boolean.FALSE);
            this.em.persist(vb);
        } catch (Exception e) {
            throw new VehicleBookingManagerException("Cannot add vehicle booking", e);
        }
    }

    public void editVehicleBooking(VehicleBookingDetailedTO to) throws VehicleBookingManagerException {
        try {
            VehicleBookingEntity vb = this.em.find(VehicleBookingEntity.class, to.getId());
            vb.setData(to);
            vb.setBookingType(this.em.find(BookingTypeEntity.class, to.getBookingType().getId()));
            vb.setEventType(this.em.find(EventTypeEntity.class, to.getEventType().getId()));
            vb.setVehicleType(this.em.find(VehicleTypeEntity.class, to.getVehicleType().getId()));
            this.em.merge(vb);
        } catch (Exception e) {
            throw new VehicleBookingManagerException("Cannot edit vehicle booking", e);
        }
    }

    public void assignVehicleBooking(VehicleBookingDetailedTO to) throws VehicleBookingManagerException {
        try {
            VehicleBookingEntity vb = this.em.find(VehicleBookingEntity.class, to.getId());
            vb.setVehicle(this.em.find(VehicleEntity.class, to.getVehicle().getId()));
            vb.setDriver(this.em.find(DriverEntity.class, to.getDriver().getId()));
            this.em.merge(vb);
        } catch (Exception e) {
            throw new VehicleBookingManagerException("Cannot edit vehicle booking", e);
        }
    }

    public void deleteVehicleBooking(Long id) throws VehicleBookingManagerException {
        try {
            VehicleBookingEntity vb = this.em.find(VehicleBookingEntity.class, id);
            vb.setDeleted(Boolean.TRUE);
            this.em.merge(vb);
        } catch (Exception e) {
            throw new VehicleBookingManagerException("Cannot delete vehicle booking", e);
        }
    }

    public List<VehicleBookingDetailedTO> getAllDetailedVehicleBookings() throws VehicleBookingManagerException {
        try {
            List<VehicleBookingDetailedTO> results = new ArrayList<VehicleBookingDetailedTO>();
            Query allVehicleBookingsQuery = em.createNamedQuery("findAllVehicleBookings");
            List dbResults = allVehicleBookingsQuery.getResultList();
            for (int i = 0; i < dbResults.size(); i++) {
                VehicleBookingEntity vb = (VehicleBookingEntity) dbResults.get(i);
                results.add(vb.getDetailedData());
            }
            return results;
        } catch (Exception e) {
            throw new VehicleBookingManagerException("Cannot get all detailed vehicle bookings", e);
        }
    }

    public VehicleBookingDetailedTO getVehicleBooking(Long id) throws VehicleBookingManagerException {
        try {
            VehicleBookingEntity vb = em.find(VehicleBookingEntity.class, id);
            return vb.getDetailedData();
        } catch (Exception e) {
            throw new VehicleBookingManagerException("Cannot get vehicle booking by ID " + id.longValue(), e);
        }
    }

    public List<VehicleBookingDetailedTO> getDetailedVehicleBookingsByFilter(VehicleBookingFilterTO filter) throws VehicleBookingManagerException {
        try {
            List<VehicleBookingDetailedTO> results = new ArrayList<VehicleBookingDetailedTO>();
            Query vehicleBookingsByFilterQuery = em.createNamedQuery("findVehicleBookingsByFilter");
            // Set driver filter
            if (filter.getDriverId() == null) {
                vehicleBookingsByFilterQuery.setParameter("driverIsNull", Boolean.TRUE);
            } else {
                vehicleBookingsByFilterQuery.setParameter("driverIsNull", Boolean.FALSE);
            }
            vehicleBookingsByFilterQuery.setParameter("driverId", filter.getDriverId());

            // Set vehicle filter
            if (filter.getVehicleId() == null) {
                vehicleBookingsByFilterQuery.setParameter("vehicleIsNull", Boolean.TRUE);
            } else {
                vehicleBookingsByFilterQuery.setParameter("vehicleIsNull", Boolean.FALSE);
            }
            vehicleBookingsByFilterQuery.setParameter("vehicleId", filter.getVehicleId());

            // Set corporate client filter
            if (filter.getCorporateClientId() == null) {
                vehicleBookingsByFilterQuery.setParameter("corporateClientIsNull", Boolean.TRUE);
            } else {
                vehicleBookingsByFilterQuery.setParameter("corporateClientIsNull", Boolean.FALSE);
            }
            vehicleBookingsByFilterQuery.setParameter("corporateClientId", filter.getCorporateClientId());

            List dbResults = vehicleBookingsByFilterQuery.getResultList();
            for (int i = 0; i < dbResults.size(); i++) {
                VehicleBookingEntity vb = (VehicleBookingEntity) dbResults.get(i);
                results.add(vb.getDetailedData());
            }
            return results;
        } catch (Exception e) {
            throw new VehicleBookingManagerException("Cannot get all detailed vehicle bookings", e);
        }
    }
}
